<template>
  <div class="form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form class="demo-form-inline" label-width="100">
      <el-row>
        <template v-for="item in formConfigure" :key="item.label">
          <el-col v-bind="collayout">
            <el-form-item
              :label="item.label"
              class="item-form"
              v-if="!item.isShow"
            >
              <template v-if="item.type == 'input'">
                <el-input
                  v-model="formData[`${item.field}`]"
                  :placeholder="item.placeholder"
                />
              </template>
              <template v-else-if="item.type == 'select'">
                <el-select
                  v-model="formData[`${item.field}`]"
                  :placeholder="item.placeholder"
                  style="width: 100%"
                >
                  <template v-for="itey in item.options" :key="itey.value">
                    <el-option :label="itey.label" :value="itey.value" />
                  </template>
                </el-select>
              </template>
              <template v-else-if="item.type == 'time'">
                <el-date-picker
                  v-model="formData[`${item.field}`]"
                  v-bind="item.otherPlaceholder"
                />
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, watch } from 'vue'
import { configType } from '@/base-ui/type/type'

export default defineComponent({
  name: 'form-pages',
  props: {
    modelValue: {
      type: Object,
      required: true
    },
    formConfigure: {
      type: Array as PropType<configType[]>,
      default: () => []
    },
    collayout: {
      type: Object,
      default: () => ({
        xl: 6,
        lg: 8,
        md: 12,
        xs: 24
      })
    }
  },
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    const formData = ref({ ...props.modelValue })
    watch(
      formData,
      (newValue) => {
        emit('update:modelValue', newValue)
      },
      { deep: true }
    )
    return {
      formData
    }
  }
})
</script>

<style lang="less" scoped>
.item-form {
  box-sizing: border-box;
  padding: 5px 50px;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
